package game.code;

import interfaces.IClient;
import interfaces.IServer;

import java.util.Collection;

import java.rmi.AccessException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class GameServer extends UnicastRemoteObject implements IServer{

	private Game game; 
	private Registry registry;
	
	public GameServer() throws RemoteException{
		game = Game.getGame();
	}
	
	public void bind(Registry registry){
		this.registry = registry;
		try {
			this.registry.bind("GameServer", this);
		} catch (AccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AlreadyBoundException e) {
			System.err.println("The registry already already binds an object under the name 'GameServer'");
			e.printStackTrace();
		}
		 
		System.out.println("GameServer running..." );
		
	}
	
	
	/**
	 * start the game, and invoke the game's loading. 
	 * 
	 */
	public void startGame(){
		game.start();
		System.out.println("Game started.");
	}
	
	
	@Override
	public int highScore() throws RemoteException {
		return game.highScore();
		
	}


	@Override
	public String highScorePlayer() throws RemoteException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int[] scores() throws RemoteException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String[] names() throws RemoteException {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public int submit(IClient who, String word) throws RemoteException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int timeLeft() throws RemoteException {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public int[] numberOfWordsByLength() throws RemoteException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Collection<Character> letters() throws RemoteException {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void twist(IClient who) throws RemoteException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int addPlayer(IClient clientObject, String name)
			throws RemoteException {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public static void main(String args[]){
		
		System.setSecurityManager(new SecurityManager());
		int portNumber = Integer.parseInt("55772");
		// First make sure there is an RMI registry running
		Registry registry = null;
		GameServer g;
		try {
			g = new GameServer();
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		try {
			g = new GameServer() ;
			//g.startGame();
			registry = java.rmi.registry.LocateRegistry.createRegistry(portNumber);
			registry.bind("GameServer", g);
			System.out.println("GameServer is running...");
		
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AlreadyBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
/*		try {			
			GameServer g = new GameServer() ;
			registry = java.rmi.registry.LocateRegistry.getRegistry(portNumber);
			g.bind(registry);

		}
		catch (Exception e) {
			try {
				GameServer g = new GameServer() ;
				registry = java.rmi.registry.LocateRegistry.createRegistry(portNumber);
				g.bind(registry);
			} catch (RemoteException e1) {
				System.out.println("Sorry, could neither contact nor create a registry running on "+portNumber);
				e1.printStackTrace();
			}
		}*/

	}
	
}
